package com.abigdreamer.swing.app;

import com.abigdreamer.swing.component.JCButton;
import com.abigdreamer.swing.component.JCLabel;
import craky.component.JImagePane;
import craky.componentc.GridBorder;
import craky.layout.LineLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.border.EmptyBorder;

public class ActionFrame
  extends JFrame
{
  private static final long serialVersionUID = 7578117962527666159L;
  protected JComponent content;
  protected JImagePane bannerPane;
  
  public ActionFrame(String title, int width, int height)
  {
    this(title, null, width, height);
  }
  
  public ActionFrame(String title, String logo, int width, int height)
  {
    _initUI(title, logo, width, height);
    
    initContent();
    
    initCenterPane();
  }
  
  private void _initUI(String title, String logo, int width, int height)
  {
    this.content = ((JComponent)getContentPane());
    this.content.setLayout(new BorderLayout());
    this.content.setBorder(new EmptyBorder(0, 0, 0, 0));
    this.content.setBackground(Color.WHITE);
    setTitle(title);
    if (logo != null) {
      setIconImage(SwingUtil.getImage(logo));
    }
    this.content.setPreferredSize(new Dimension(width, height));
    
    setDefaultCloseOperation(3);
    setResizable(false);
  }
  
  protected JPanel centerPane = new JPanel();
  JCButton btnOk = new JCButton("确定");
  JCButton btnCancel = new JCButton("取消");
  protected JImagePane bottomPane;
  
  protected void onButtonOKClick() {}
  
  protected void onButtonCancleClick() {}
  
  protected void initCenterPane() {}
  
  private void initContent()
  {
    this.btnOk.setPreferredSize(new Dimension(69, 21));
    
    getRootPane().setDefaultButton(this.btnOk);
    
    this.btnOk.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        ActionFrame.this.onButtonOKClick();
      }
    });
    this.btnCancel.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        ActionFrame.this.onButtonCancleClick();
      }
    });
    this.btnCancel.setPreferredSize(this.btnOk.getPreferredSize());
    
    this.bannerPane = new JImagePane();
    this.bannerPane.setPreferredSize(new Dimension(334, 64));
    this.bannerPane.setMode("Tiled");
    
    this.bottomPane = new JImagePane();
    this.bottomPane.setPreferredSize(new Dimension(334, 30));
    this.bottomPane.setImage(SwingUtil.getImage("login_bottom.png"));
    this.bottomPane.setBorder(new EmptyBorder(0, 0, 0, 0));
    this.bottomPane.setLayout(new LineLayout(5, 0, 7, 5, 0, 10, 10, 0));
    this.bottomPane.add(this.btnOk, "End");
    this.bottomPane.add(this.btnCancel, "End");
    
    this.centerPane.setBackground(new Color(228, 244, 255));
    this.centerPane.setBorder(new GridBorder(Color.WHITE, 0, 1, 0, 1));
    this.centerPane.setLayout(new LineLayout(0, 0, 0, 0, 0, 10, 10, 1));
    

    this.content.add(this.bannerPane, "North");
    this.content.add(this.bottomPane, "South");
    this.content.add(this.centerPane, "Center");
  }
  
  protected JCLabel createLabel(String text)
  {
    JCLabel label = new JCLabel(text);
    label.setBorder(new EmptyBorder(1, 0, 0, 0));
    label.setPreferredSize(new Dimension(74, 24));
    label.setHorizontalAlignment(4);
    
    return label;
  }
  
  protected JCLabel createLinkedLabel(String text)
  {
    JCLabel label = new JCLabel(text);
    label.setBorder(new EmptyBorder(1, 3, 0, 0));
    label.setPreferredSize(new Dimension(75, 24));
    label.setForeground(new Color(22, 112, 235));
    label.setCursor(new Cursor(12));
    
    return label;
  }
}
